const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '', //手机号码
    code: '', //验证码
    getCodeInterval: 0, //获取验证码倒计时
    isShowSms: false, //弹窗控制值
    btnCodeTxt: '获取验证码', //获取验证码按钮的文字
  },

  /**
   * 隐藏短信验证手机窗口
   */
  hideSmsDialog() {
    let _this = this;
    _this.setData({
      isShowSms: false
    })
  },

  /**
   * 显示短信验证手机窗口
   */
  showSmsDialog() {
    let _this = this;
    _this.setData({
      isShowSms: true
    })
  },

  /**
   * 绑定输入手机号码
   */
  bindInputPhone(e) {
    let _this = this;
    _this.setData({
      phone: e.detail.value,
    })
  },

  /**
   * 绑定输入验证码
   */
  bindInputCode(e) {
    let _this = this;
    _this.setData({
      code: e.detail.value,
    })
  },

  /**
   * 发送验证码
   */
  sendCode() {
    let _this = this;
    if (!(/^1[3456789]\d{9}$/.test(_this.data.phone))) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    wx.showLoading({title:'加载中', mask:true});
    App._post_form('phone/sms', {
      phone: _this.data.phone
    }, result => {
      wx.hideLoading();
      _this.setData({
        getCodeInterval: 60,
      })
      let i = setInterval(() => {
        _this.setData({
          btnCodeTxt: _this.data.getCodeInterval + 's重新获取'
        })
        _this.data.getCodeInterval--
        if (_this.data.getCodeInterval == 0) {
          _this.setData({
            btnCodeTxt: '获取验证码',
          })
          clearInterval(i)
        }
      }, 1000)
    })
  },

  /**
   * 提交短信验证码 验证手机
   */
  verifyPhone() {
    let _this = this;
    wx.showLoading({title:'加载中', mask:true});
    App._post_form('phone/verify', {
      phone: _this.data.phone,
      code: _this.data.code,
      referee_id: wx.getStorageSync('referee_id')
    }, function (result) {
      if (result.data.verify_status == 1) {
        wx.showToast({
          title: '绑定成功',
          icon: 'success',
          duration: 2000,
          success() {
            setTimeout(function () {
              _this.navigateBack()
            }, 2000)
          }
        })
      } else {
        wx.showToast({
          title: result.msg,
          icon: 'none',
          duration: 3000,
        })
      }
    })
  },

  /**
   * 返回上一页
   */
  navigateBack() {
    let _this = this;
    if (_this.data.options.login == 1) {
      wx.navigateBack({
        delta: 2
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },

  /**
   * 微信授权手机
   */
  bindGetPhoneNumber: function(o) {
    var that=this;
    "getPhoneNumber:fail user deny" == o.detail.errMsg ? App.args.bus.showToast('未授权') : that.decodePhone(o,that.data.code)//console.log('手机授权数据',o,that.data.code)
  },
  /*bindGetPhoneNumber(e) {
    let _this = this;
    if (e.detail.iv && e.detail.encryptedData) {
      wx.showLoading({title:'加载中', mask:true});
      App._post_form('phone/authorize', {
        encrypted_data: e.detail.encryptedData,
        iv: e.detail.iv,
        //referee_id: wx.getStorageSync('referee_id')
      }, result => {
        wx.showToast({
          title: '绑定成功',
          icon: 'success',
          duration: 2000,
          success() {
            setTimeout(function () {
              _this.navigateBack()
            }, 2000)
          }
        })
      })
    }
  },*/

  decodePhone(o,code){
    var that=this;
    App.args.request({
      url: App.args.api.user.user_binding,
      data: {
        iv: o.detail.iv,
        encryptedData: o.detail.encryptedData,
        code: code
      },
      success(e) {
        1 == e.code ? that.getAppUserInfo(e.data.phone) : App.args.bus.showToast('授权失败,请重试')
      }
    });
  },
  getAppUserInfo(p){
    var that = this;
    App.args.request({
      url:App.args.api.user.customer_wx_info,
      data:{
        phone:p,
      },
      success(e){
        if(e.code==1){
          App.args.bus.showToast('授权成功')
          wx.setStorageSync('appUserInfo',e)
          wx.setStorageSync('USER_INFO',e)
          that.navigateBack()
        }else {
          App.args.bus.showToast('授权失败,请重试')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    _this.setData({
      options: options
    })
    console.log(_this.data.options.login)
    wx.login({
      success(e){
        console.log(e)
        _this.setData({code:e.code})
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    /*let _this = this;
    // 检查登录态是否失效 确保手机号正确解码
    wx.checkSession({
      success() {
        console.log('登录态正常')
      },
      fail() {
        App.doLogin()
      }
    })*/
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

})
